/*
 * the codes was writted by taing, but you can copy or moidfy it
 */

package com.taing.sample.web;

import com.taing.autosql.dao.IDataSqlDao;
import com.taing.autosql.mapper.IMapList2EntityMapper;
import com.taing.autosql.model.DataSqlModel;
import com.taing.autosql.sqlconfig.IAutoSqlDao;
import com.taing.sample.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author tianyi
 * @Description 通用的对外接口
 * @Date 2023 - 01 - 30
 **/
@RestController
@RequestMapping("/autoserive")
public class AutoSqlControl<T> {

//    @Autowired
//    IDataSqlService dataSqlService;

    @Autowired
    private IAutoSqlDao autosqlDao;
    @Autowired
    private IDataSqlDao dataSqlDao;
    @Autowired
    private IMapList2EntityMapper<UserInfo> userInfoMapper;

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public DataSqlModel saveDataSql() {
        DataSqlModel dataSqlModel = new DataSqlModel();
        dataSqlModel.setSqlContext("select * from my_user_t");
        dataSqlModel.setMethod("findAllUserInfo");
        dataSqlModel.setDeleteFlag("N");
        dataSqlModel.setCreationDate(new Date());
        dataSqlModel.setLastUpdateDate(new Date());
        return dataSqlDao.save(dataSqlModel);
    }

    @RequestMapping(value = "/dataSqlAll/{methodName}", method = RequestMethod.POST)
    public List<T> getDataSql(@PathVariable String methodName, @RequestBody Map<String, Object> requestParam) throws InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException, NoSuchFieldException {
        requestParam.put("methodName", methodName);
        return autosqlDao.queryServiceList(requestParam);
    }
}
